import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pax_info_app/model/sys_info.dart';
import 'package:pax_info_app/utils/uidata.dart';
import 'package:webview_flutter/webview_flutter.dart';

class WebViewPage extends StatefulWidget {
  final String? title;
  final String url;
  WebViewPage({this.title, required this.url});
  @override
  _WebViewPageState createState() => _WebViewPageState();
}

class _WebViewPageState extends State<WebViewPage> {
  bool _loading = false;

  final Completer<WebViewController> _controller =
      Completer<WebViewController>();
  late WebViewController _ctrl;

  @override
  void initState() {
    _loading = true;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: UIData.windowBg,
      appBar: AppBar(
        backgroundColor: Color.fromRGBO(143, 148, 251, 1),
        title: Text('假期管理'),
      ),
      body: Builder(builder: (BuildContext context) {
        return Stack(
          children: [
            WebView(
              initialUrl: widget.url,
              javascriptMode: JavascriptMode.unrestricted,
              onWebViewCreated: (WebViewController webViewController) {
                _ctrl = webViewController;
                _controller.complete(webViewController);
              },
              // TODO(iskakaushik): Remove this when collection literals makes it to stable.
              // ignore: prefer_collection_literals
              // javascriptChannels: <JavascriptChannel>[
              //   _toasterJavascriptChannel(context),
              // ].toSet(),
              navigationDelegate: (NavigationRequest request) {
                if (request.url.startsWith('https://www.youtube.com/')) {
                  print('blocking navigation to $request}');
                  return NavigationDecision.prevent;
                }
                print('allowing navigation to $request');
                return NavigationDecision.navigate;
              },
              onPageStarted: (String url) {
                print('Page started loading: $url');
              },
              onPageFinished: (String url) {
                print('Page finished loading: $url');
                String cookie =
                    "document.cookie = 'info2=y2mfiv9b3g1h0hUpgSVMeay+Cxl1x8r7CVOWNxMi/17nd8szhpf1Rg==';document.cookie = 'ASP.NET_SessionId=zoanxy453pahd3fggnlpkde4'";
                _ctrl.evaluateJavascript(cookie);
                setState(() {
                  _loading = false;
                });
              },
              gestureNavigationEnabled: true,
            ),
            _loading
                ? Container(
                    width: SysInfo.instance.deviceSize.width,
                    height: SysInfo.instance.deviceSize.height,
                    color: UIData.white,
                    child: Center(
                      child: CircularProgressIndicator(),
                    ),
                  )
                : Stack()
          ],
        );
      }),
    );
  }
}
